package com.py.net.server.businese;

import com.google.protobuf.MessageLite;
import com.py.net.proto.ChatProbuf.CTSLogin;
import com.py.net.proto.ChatProbuf.STCLogin;
import com.py.net.bean.Gloable;
import com.py.net.bean.User;
import com.py.net.proto.IBusineseHandle;

public class ServerLoginHandle extends IBusineseHandle<CTSLogin> {

	@Override
	public MessageLite handle(CTSLogin msg) {
		//登陆逻辑
		String name = msg.getName();
		String pwd = msg.getPwd();
		//验证用户名密码
		STCLogin.Builder builder = STCLogin.newBuilder();
		if (session.user != null && name.equals(session.user.name)) {
			builder.setStatus(0);
		} else {
			if (check(name, pwd)) {
				builder.setStatus(0);
				User user = new User(session);
				user.id = name;
				user.name = name;
				session.setUser(user);
				Gloable.userMap.put(user.id, user);
			} else {
				builder.setStatus(1);
			}
		}
		return builder.build();
	}
	
	private static boolean check(String name, String pwd) {
		//TODO 验证用户名密码逻辑
		if (name.startsWith("cyd")) {
			return true;
		}
		return false;
	}

}
